<?php
class Users extends CI_Controller
{
	function Users()
	{
		parent::__construct();	
		$this->load->model('user_detail_model', 'users_tbl');



	}
	function getPath($action = ''){
		return array('controller' => strtolower(__CLASS__), 'action' => strtolower($action));
	}
	function index($page = 0)
	{
		$per_page_count       = $this->config->item('per_page_count');
		$users                = $this->users_tbl->getUsers($page, $per_page_count);
		$view = $this->getPath(__FUNCTION__);
		$view['users'] 		= $users->records;
		$view['cur_page'] 	= $page;
		$view['title']		= 'Users Listing';
		$view['subtitle']	= 'Users Listing';
		/** Pagination block **/
		$config['base_url'] 	= $this->config->item('base_url').$this->config->item('admin_base').'/users/index/';
		$config['total_rows'] 	= $users->total;
		$config['per_page'] = $per_page_count;
		$config['cur_page'] = $page /$per_page_count;
		$config['uri_segment'] 	= 4;
		$config['num_links'] 	= 3;
		$this->pagination->initialize($config);
		$view['msg'] 	= $this->lang->line($this->session->flashdata('users_msg'));
		
		$this->layout->adminview('users/view', $view);
		
	}
	function add($cur_page = 0)
	{
		$view = $this->getPath(__FUNCTION__);
		$view['title'] = "Add Users";
		$view['page_title'] = "Add Users";
		$save = $this->input->post('save');
		$view['users'] 	= array();
		$view['cur_page'] = $cur_page;
		$view['subtitle']	= 'Add Users';
		$where                    = array('deleted' => 0);
		$group                   = $this->group_model->getGroups($page, $per_page_count , $like, $where );
		$view['group'] 		  = $group->records;
		if(!empty($save)) {
			$data = $this->retrieve->post($this->users_tbl->fields);
			$insertStatus = $this->users_tbl->add($data);
			
			$data1['user_id'] = $insertStatus->user_id;
			//$data1['group_id'] =  $this->input->post('group');//$this->retrieve->post($this->users_tbl->fields);
			//$count=count($data1['group_id']);
			foreach($view['group'] as $g)
			{
				$data2['user_id'] = $data1['user_id'];
				$group = $this->input->post('group'.$g['id']);
				$data2['group_id'] = $g['id'];
				if(!empty($group))
				{
					$data2['flag'] = 1;
				$insertStatus = $this->admin_group_model->add($data2);
				}
				else
				{
					$data2['flag'] = 0;
				$insertStatus = $this->admin_group_model->add($data2);
				}
			}
			//for($i=0;$i<$count;$i++)
			//{
				//$data2['user_id']   = $data1['user_id'];
				//$data2['group_id']  = $data1['group_id'][$i];
			//$insertStatus = $this->admin_group_model->add($data2);
			//}
			//print_r($data1);
			//print $insertStatus;exit;
			$view['users'] 	= $data;
			switch($insertStatus->status){
				case 1:
					$this->session->set_flashdata('users_msg', 'users_add_success');
					if(!empty($cur_page)) 
						redirect($this->config->item('admin_base').'users/index/'.$cur_page,'location');
					else
						redirect($this->config->item('admin_base').'users/index/','location');
					exit;		
					break;
				case -1:
					break;
				case -2:
					$view['msg'] 	= $this->lang->line('users_valid_failed');
					break;
				case -3:
					$view['msg'] 	= $this->lang->line('users_already_exists');
					break;
				case false:
					$view['msg'] 	= $this->lang->line('users_add_failure');
	
					break;
				
				default:
					$view['msg'] 	= $this->lang->line('users_add_failure');
					break;
			}
		} 
		
		$this->layout->adminview('users/addedit', $view);
	}
	
	function edit($id = '', $cur_page='')
	{
		$view = $this->getPath(__FUNCTION__);
		 $where                    = array('deleted' => 0);
		    $group                    = $this->group_model->getGroups($page, $per_page_count , $like, $where );
		    $view['group'] 		      = $group->records;
		if(empty($id)) {
			redirect($this->config->item('admin_base').'users/index/'.$cur_page,'location');
			exit;
		} else {
			$records = $this->users_tbl->getUsers(0,1, array('id' => $id));
			$users = $records->records[0];
			$records = $this->admin_group_model->getAdminGroups(0,'','', array('user_id' => $id,'flag'=>1));
			$view['groups'] = $records->records;
			//print_r($records);
			if(empty($users)) {
				$this->session->set_flashdata('users_msg', 'users_invalid_record');
				redirect($this->config->item('admin_base').'users/index','location');
				exit;
			}
			$view['id']		= $id;
			list($year,$month,$day) = explode('-',$users['dob']);
			$users['dobdd'] = $day;
			$users['dobmm'] = $month;
			$users['dobyy'] = $year;
			$view['user']	= $users;
		}
		
		$view['title'] = "Edit Users ";
		$view['page_title'] = "Edit Users";
		$view['subtitle']	= 'Edit Users';
		$save = $this->input->post('save');
		if(!empty($save)) {
			$data = $this->retrieve->post($this->users_tbl->fields);
			$insertStatus = $this->users_tbl->udpate($id, $data);
			$data1['user_id'] = $id;
			$data1['group_id'] =  $this->input->post('group');
		   
			foreach($view['group'] as $g)
			{
				$data2['user_id'] = $data1['user_id'];
				$group = $this->input->post('group'.$g['id']);
				$data2['group_id'] = $g['id'];
				if(!empty($group))
				{
					$data2['flag'] = 1;
				$insertStatus = $this->admin_group_model->update($id,$g['id'],$data2);
				}
				else
				{
					$data2['flag'] = 0;
					$insertStatus = $this->admin_group_model->update($id,$g['id'], $data2);
				}
			}
			switch($insertStatus){
				case 1:
					$this->session->set_flashdata('users_msg', 'users_edit_success');
					redirect($this->config->item('admin_base').'users/index/'.$cur_page,'location');
					exit;		
					break;
				case -1:
					break;
				case -3:
					$view['msg'] 	= $this->lang->line('users_already_exists');
					break;
				case -2:
					$view['msg'] 	= $this->lang->line('users_valid_failed');
					break;
				case false:
					$view['msg'] 	= $this->lang->line('users_edit_failure');
					break;
				;
				default:
					$view['msg'] 	= $this->lang->line('users_edit_failure');
					break;
			}
			$view['users'] 	= $data;
		}
		$view['cur_page'] = $cur_page; 
		
		$this->layout->adminview('users/addedit', $view);
	}
	
	
	function delete($id = '', $cur_page)
	{
		if(empty($id)) {
			redirect($this->config->item('admin_base').'users/index/'.$cur_page,'location');
			exit;
		} else {
			$deleteStatus = $this->users_tbl->delete($id);
			switch($deleteStatus){
				case 1:
					$this->session->set_flashdata('users_msg', 'users_delete_success');
					redirect($this->config->item('admin_base').'users/index/'.$cur_page,'location');
					exit;		
					break;
				case false:
					$view['msg'] 	= $this->lang->line('users_delete_failure');
					break;
			}
		}
		
	}
	
	function cancel(){
    redirect($this->config->item('admin_base').'users/index','location');
		exit;	
	}
	
	function login(){
		$view = array();
		$this->load->helper(array('form', 'url'));
		$this->layout->adminview('users/login', $view);
	}
	function login_suc() {
		print_r($_POST);
		$this->users_tbl->login($_POST['username'], $_POST['password']);
	}
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */